home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-games-data / glchess / chess / fics / telnet.py < prev   
Encoding:
Python Source  |  2009-04-14  |  5.4 KB  |  208 lines

  1. # -*- coding: utf-8 -*-
  2. # -*- coding: utf-8 -*-
  3.  
  4. class Decoder:
  5.     """
  6.     """
  7.     
  8.     buffer = ''
  9.     processing = False
  10.  
  11.     def __init__(self):
  12.         self.commands = {240: (self.onEndSubnegotiation, False),
  13.                          241: (self.onNoOp, False),
  14.                          242: (self.onDataMark, False),
  15.                          243: (self.onBreak, False),
  16.                          244: (self.onInterruptProcess, False),
  17.                          245: (self.onAbortOutput, False),
  18.                          246: (self.onAreYouThere, False),
  19.                          247: (self.onEraseCharacter, False),
  20.                          248: (self.onEraseLine, False),
  21.                          249: (self.onGoAhead, False),
  22.                          250: (self.onStartSubnegotiation, False),
  23.                          251: (self.onWill, True),
  24.                          252: (self.onWont, True),
  25.                          253: (self.onDo, True),
  26.                          254: (self.onDont, True)}
  27.     
  28.     def onData(self, data):
  29.         """Called when data from the telnet stream is decoded.
  30.         
  31.         'data' is the data inside the telnet stream (str).
  32.         """
  33.         pass
  34.     
  35.     def onEndSubnegotiation(self):
  36.         pass
  37.     
  38.     def onNoOp(self):
  39.         pass
  40.     
  41.     def onDataMark(self):
  42.         pass
  43.     
  44.     def onBreak(self):
  45.         pass
  46.     
  47.     def onInterruptProcess(self):
  48.         pass
  49.     
  50.     def onAbortOutput(self):
  51.         pass
  52.     
  53.     def onAreYouThere(self):
  54.         pass
  55.     
  56.     def onEraseCharacter(self):
  57.         pass
  58.     
  59.     def onEraseLine(self):
  60.         pass
  61.     
  62.     def onGoAhead(self):
  63.         pass
  64.     
  65.     def onStartSubnegotiation(self):
  66.         pass
  67.     
  68.     def onWill(self, option):
  69.         pass
  70.     
  71.     def onWont(self, option):
  72.         pass
  73.     
  74.     def onDo(self, option):
  75.         pass
  76.  
  77.     def onDont(self, option):
  78.         pass
  79.     
  80.     def onUnknownCommand(self, command):
  81.         pass
  82.     
  83.     def registerIncomingData(self, data):
  84.         """Register data received from a telnet device.
  85.         
  86.         'data' is the received data (str).
  87.         """
  88.         if self.processing:
  89.             self.buffer += data
  90.             return
  91.         self.processing = True
  92.         
  93.         d = self.buffer + data
  94.         self.buffer = ''
  95.         while True:
  96.             # Process any data registered while in this method
  97.             d += self.buffer
  98.             if len(d) == 0:
  99.                 break
  100.             
  101.             index = d.find('\xff')
  102.             if index < 0:
  103.                 self.onData(d)
  104.                 break
  105.  
  106.             # Use the data up to this point
  107.             text = d[:index]
  108.             if len(text) > 0:
  109.                 self.onData(text)
  110.             
  111.             # Try and get the command
  112.             try:
  113.                 command = ord(d[index+1])
  114.             except IndexError:
  115.                 self.buffer = d[index:]
  116.                 break
  117.  
  118.             # Special case - repeated 0xFF is just 0xFF
  119.             if command == 255:
  120.                 self.onData('\xff')
  121.                 d = d[index+2:]
  122.                 continue
  123.             
  124.             # Look for the command
  125.             try:
  126.                 (method, hasParameter) = self.commands[command]
  127.             except KeyError:
  128.                 self.onUnknownCommand(command)
  129.                 d = d[index+2:]
  130.                 continue
  131.  
  132.             if hasParameter:
  133.                 try:
  134.                     parameter = ord(d[index+2])
  135.                 except IndexError:
  136.                     self.buffer = d[index:]
  137.                     break
  138.                 method(parameter)
  139.                 d = d[index+3:]
  140.             else:
  141.                 method()
  142.                 d = d[index+2:]
  143.             
  144.         self.processing = False
  145.         
  146. if __name__ == '__main__':
  147.     class D(Decoder):
  148.         def onData(self, data):
  149.             print 'onData(' + repr(data) + ')'
  150.             
  151.         def onEndSubnegotiation(self):
  152.             print 'onEndSubnegotiation()'
  153.     
  154.         def onNoOp(self):
  155.             print 'onNoOp()'
  156.     
  157.         def onDataMark(self):
  158.             print 'onDataMark()'
  159.         
  160.         def onBreak(self):
  161.             print 'onBreak()'
  162.         
  163.         def onInterruptProcess(self):
  164.             print 'onInterruptProcess()'
  165.         
  166.         def onAbortOutput(self):
  167.             print 'onAbortOutput()'
  168.         
  169.         def onAreYouThere(self):
  170.             print 'onAreYouThere()'
  171.         
  172.         def onEraseCharacter(self):
  173.             print 'onEraseCharacter()'
  174.         
  175.         def onEraseLine(self):
  176.             print 'onEraseLine()'
  177.     
  178.         def onGoAhead(self):
  179.             print 'onGoAhead()'
  180.         
  181.         def onStartSubnegotiation(self):
  182.             print 'onStartSubnegotiation()'
  183.     
  184.         def onWill(self, option):
  185.             print 'onWill(%i)' % option
  186.     
  187.         def onWont(self, option):
  188.             print 'onWont(%i)' % option
  189.     
  190.         def onDo(self, option):
  191.             print 'onDo(%i)' % option
  192.         
  193.         def onDont(self, option):
  194.             print 'onDont(%i)' % option
  195.     
  196.         def onUnknownCommand(self, command):
  197.             print 'onUnknownCommand(%i)' % command
  198.     
  199.     d = D()
  200.     d.registerIncomingData('A telnet\xff\xff string \xff\xf3 with a break and a DO 6 \xff\xfd\x06. Fun innit?')
  201.  
  202.     d.registerIncomingData('ABC\xff')
  203.     d.registerIncomingData('\xf3DEF')
  204.     
  205.     d.registerIncomingData('ABC\xff')
  206.     d.registerIncomingData('\xfd')
  207.     d.registerIncomingData('\x0aDEF')
  208.